//
//  SSTabBar.swift
//  weibo
//
//  Created by 邵帅 on 16/6/23.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSTabBar: UITabBar {
    // 按钮点击回调闭包
    var closure : (()->())?
    
    
    // 添加按钮
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 重新布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let width = bounds.width / 5
        let height = bounds.height
        var index : Int = 0
        
        
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectMake(CGFloat(index) * width, 0, width, height)
                
                index = index + 1
                
                if index == 2 {
                    index = index + 1
                }
            }
        }
        composeButton.frame = CGRectMake(width * 2, 0, width, height)
    }
    
    

    // 懒加载按钮
    private lazy var composeButton : UIButton = {
        
        let button = UIButton(type: .Custom)
        
        button.setBackgroundImage(UIImage(named : "tabbar_compose_button") , forState: .Normal)
        button.setBackgroundImage(UIImage(named : "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: #selector(SSTabBar.btnClick), forControlEvents: .TouchUpInside)
        
        return button
    }()

    // 点击回调
    @objc private func btnClick() -> Void {
        
        closure?()
    }
}
